﻿
$.fn.EasyUiTree = {
    Init: function (options) {

        options.id = $.SetAttValue(options, "id", 'tree');
        options.url = $.SetAttValue(options, "url", 'Json.ashx');
        options.dnd = $.SetAttValue(options, "dnd", true);
        options.menuID = $.SetAttValue(options, "menuID", "menu");
        options.data = $.SetAttValue(options, "data", null);
        options.onClickUrl = $.SetAttValue(options, "onClickUrl", "");

        options.UrlSelect = options.url + "?tn=" + options.tn + "&mode=select&level=2";
        if ($.getUrlParamer("value", "") != "")
        {
            options.UrlSelect = options.url + "?tn=" + options.tn + "&mode=select&level=2&value=" + $.getUrlParamer("value", "");
        }

        options.data = options.data;
        //options.UrlSelectLevel = options.url + "?tn=" + tn + "&mode=select&Level=@level";
        options.UrlNumSelect = options.url + "?tn=" + options.tn + "&mode=NumSelect";

        options.UrlEdit = options.url + "?tn=" + options.tn + "&mode=edit";
        options.UrlMove = options.url + "?tn=" + options.tn + "&mode=move";
        options.UrlInsert = options.url + "?tn=" + options.tn + "&mode=insert";
        options.UrlDelete = options.url + "?tn=" + options.tn + "&mode=delete";
        options.UrlUp = options.url + "?tn=" + options.tn + "&mode=up";
        options.UrlDown = options.url + "?tn=" + options.tn + "&mode=down";
        if (options.UrlUpdate == undefined) {
            options.UrlUpdate = "right.aspx";
        }

        options.UrlUpdate = options.UrlUpdate + "?id=@id&tn=" + options.tn;//更新节点其它数据

        options.TreeID = '#' + options.id;
        options.NodeText;//用于记录编辑前后名称对比

        $.fn.EasyUiTree.Ready(options);
    },

    Ready: function (options) {
        $(options.TreeID).tree({
            url: options.data != null ? null : options.UrlSelect,
            data: options.data,
            lines: true,
            animate: true,
            dnd: options.dnd,
            onContextMenu: function (e, node) {
                e.preventDefault();
                $(options.TreeID).tree('select', node.target);
                $('#menu').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onClick: function (node) {
                var url = options.UrlUpdate.replace("@id", node.id);
                if (node.attributes != undefined) {
                    if (node.attributes.url != "") {
                        url = node.attributes.url;
                        url = url.replace("@id", node.id);
                    }
                }
                $('#iframeRight').attr("src", options.onClickUrl + url);
            },
            onDblClick: function (node) {
                var url = options.UrlUpdate.replace("@id", node.id);
                $('#iframeRight').attr("src", url);
            },
            onBeforeEdit: function (node)//编辑之前
            {
                options.NodeText = node.text;
            },
            onAfterEdit: function (node)//在编辑节点之后触发。
            {
                if (node.text != options.NodeText)//名称不变的时候 不做更新处理
                {
                    $.post(options.UrlEdit, { "id": node.id, "name": node.text }, function (result) {
                        //var pnode = ($(options.TreeID).tree('getParent', node.target));
                        //$(options.TreeID).tree('reload', pnode.target);
                    });
                }
            },
            onDrop: function (target, source, point) {
                var targetNode = $(options.TreeID).tree('getNode', target);
                $.post(options.UrlMove, { "id": source.id, "targetId": targetNode.id, "point": point }, function (result) {
                    if (result == "refresh")//拖动到原来的父节点 不做处理
                    {
                        $(options.TreeID).tree('reload', targetNode.target);
                    }
                });
            }
        });
    },

    /*添加节点*/
    Add: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        $.post(options.UrlInsert+"&pid="+node.id, { "pid": node.id, "Name": '新建节点' }, function (result) {
            $(options.TreeID).tree('append',
                {
                    parent: node.target,
                    data: [{ "id": result, text: "新建节点" }]
                });
            node = $(options.TreeID).tree('find', result);
            $(options.TreeID).tree('beginEdit', node.target);
        });
    },

    /*删除*/
    Remove: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        var isLeaf = $(options.TreeID).tree("isLeaf", node.target);
        var getRoot = $(options.TreeID).tree("getRoot");//跟节点

        var l = false; var messager = "";
        switch (node.id != getRoot.id)//跟节点
        {
            case true:
                {
                    switch (isLeaf)//只能删除叶子
                    {
                        case true:
                            {
                                messager = "确认删除【" + node.text + "】的节点吗？";
                                l = true;
                                break;
                            }
                        case false:
                            {
                                messager = "此节点存在子节点，不能删除！";
                                break;
                            }
                    }
                    break;
                }
            case false:
                {
                    l = false;
                    messager = "根节点不能删除！";
                    break;
                }
        }

        switch (l) {
            case true:
                {
                    $.messager.confirm("提示", messager, function (r) {
                        if (r) {
                            $.post(options.UrlDelete, { "id": node.id }, function (result) {
                                $(options.TreeID).tree('remove', node.target);//【参数：target】移除一个节点和它的子节点，target参数是一个节点DOM对象。
                            });
                        }
                    });
                    break;
                }
            case false:
                {
                    $.messager.alert("提示", "<div style='line-height:28px;'>" + messager + "</div>", "warning");
                }
        }
    },

    /*上移*/
    Up: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        $.post(options.UrlUp, { "id": node.id }, function (result) {
            if (result == "NO") {
                $.messager.alert("提示", "<div style='line-height:28px;'>已经是最顶部了！</div>", "warning");
            }
            else {
                var pnode = ($(options.TreeID).tree('getParent', node.target));
                $(options.TreeID).tree('reload', pnode.target);
            }
        });
    },

    /*下移*/
    Down: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        $.post(options.UrlDown, { "id": node.id }, function (result) {
            if (result == "NO") {
                $.messager.alert("提示", "<div style='line-height:28px;'>已经是最底部了！</div>", "warning");
            }
            else {
                var pnode = ($(options.TreeID).tree('getParent', node.target));
                $(options.TreeID).tree('reload', pnode.target);
            }
        });
    },

    /*编辑*/
    Edit: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        $(options.TreeID).tree('beginEdit', node.target);
    },

    /*刷新节点*/
    Refresh: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        $(options.TreeID).tree('reload', node.target);
    },
    /*复制*/
    Copy: function (options) {
        var node = $(options.TreeID).tree('getSelected');
        $("#iframeRight").contents().find("#关联词TextBox").val(node.id);
    }

    ///*打开第几个级别*/
    //ExpandLevel: function (options) {
    //    $(options.TreeID).tree({
    //        url: options.UrlSelect + "&Level=" + Level
    //    });
    //    $(options.TreeID).tree('reload');
    //}

};